<template>
	<el-card shadow="hover">
		<el-form :model="queryParams" ref="queryFormRef" :inline="true" v-show="showSearch">
			<el-form-item label="名称" prop="title">
				<el-input v-model="queryParams.title" placeholder="名称" clearable />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" @click="onQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" @click="onResetQuery">重置</el-button>
			</el-form-item>
		</el-form>
		<el-row :gutter="10" class="top-block mb8">
			<el-col :span="1.5">
				<el-button @click="onGeneral" type="primary" plain icon="el-icon-plus">通用弹窗</el-button>
			</el-col>
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
		</el-row>
		<el-table :data="tableList" v-loading="loading" @selection-change="onSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="用户ID" align="center" prop="id" />
			<el-table-column label="用户昵称" align="center" prop="nickname" />
		</el-table>
		<pagination-component
			v-show="total > 0"
			:total="total"
			v-model:page="queryParams.pageNum"
			v-model:limit="queryParams.pageSize"
			@pagination="getList"
		/>
		<general-dialog ref="generalRef" />
	</el-card>
</template>

<script lang="ts">
import { onMounted, reactive, ref, toRefs } from 'vue';

import GeneralDialog from './dialog/GeneralDialog.vue';

export default {
	name: 'generalPage',
	components: { GeneralDialog },
	setup() {
		const queryFormRef = ref(null);
		const generalRef = ref(null);
		// 被选择的数据项
		let selectionInfo: Array<any> = [];
		const state = reactive({
			queryParams: {
				title: '',
				pageNum: 1,
				pageSize: 10,
			},
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 选中数组
			ids: [],
			showSearch: true,
			tableList: [],
			loading: false,
			total: 20,
		});

		onMounted(() => {
			getList();
		});

		/********** 点击事件(on开头) **********/
		// 搜索
		const onQuery = () => {
			state.queryParams.pageNum = 1;
			getList();
		};
		// 重置
		const onResetQuery = () => {
			(queryFormRef.value as any)?.resetFields();
			onQuery();
		};
		// 通用弹窗
		const onGeneral = () => {
			(generalRef.value as any)?.openDialog({}, () => getList());
		};
		// 多选框选中数据
		const onSelectionChange = (selection: any) => {
			selectionInfo = selection;
			state.single = selection.length !== 1;
			state.multiple = !selection.length;
		};

		/********** 方法事件 **********/
		const getList = () => {
			state.loading = true;
		};

		/********** 网络请求 **********/

		return {
			queryFormRef,
			generalRef,
			onQuery,
			onResetQuery,
			onSelectionChange,
			onGeneral,
			getList,
			...toRefs(state),
		};
	},
};
</script>

<style lang="scss" scoped></style>
